package com.offcn.sellergood.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.entity.PageResult;
import com.offcn.sellergood.pojo.Brand;
import com.offcn.sellergood.dao.BrandMapper;
import com.offcn.sellergood.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {

    @Autowired
    private  BrandMapper brandMapper;
    @Override
    public List<Brand> findAll() {
        return  this.list();
    }

    @Override
    public Brand findById(Long id) {
       return this.getById(id);
    }

    @Override
    public void add(Brand brand) {
        this.save(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        this.updateById(brand);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }
/////////////////////////////////////////
    @Override
    public List<Brand> findList(Brand brand) {
        //模糊查询
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if(brand != null){
            if (brand.getId() != null){
                queryWrapper.eq("id",brand.getId());
            }
            if (!StringUtils.isEmpty(brand.getName())){
                queryWrapper.like("name",brand.getName());
            }
            if(!StringUtils.isEmpty(brand.getFirstChar())){
                queryWrapper.eq("first_char",brand.getFirstChar());
            }
            if(!StringUtils.isEmpty(brand.getImage())){
                queryWrapper.eq("image",brand.getImage());
            }
        }
        return this.list(queryWrapper);
    }

    @Override
    public PageResult<Brand> findPage(int pageNo, int pageSize) {
        Page<Brand> objectPage = new Page<>();
        Page<Brand> page = this.page(objectPage, new QueryWrapper<Brand>());
        return new PageResult<Brand>(page.getTotal(),page.getRecords());
    }
    //分页家条件
    @Override
    public PageResult<Brand> findPage(Brand brand, int pageNo, int pageSize) {
        Page<Brand> objectPage = new Page<>();
        //模糊查询
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if(brand != null){
            if (brand.getId() != null){
                queryWrapper.eq("id",brand.getId());
            }
            if (!StringUtils.isEmpty(brand.getName())){
                queryWrapper.like("name",brand.getName());
            }
            if(!StringUtils.isEmpty(brand.getFirstChar())){
                queryWrapper.eq("first_char",brand.getFirstChar());
            }
            if(!StringUtils.isEmpty(brand.getImage())){
                queryWrapper.eq("image",brand.getImage());
            }
        }
        Page<Brand> page = this.page(objectPage, queryWrapper);
        return new PageResult<Brand>(page.getTotal(),page.getRecords());
    }

    @Override
    public List<Map> selectOptions() {
       return brandMapper.selectOptions();
    }
}
